package com.demo.project_template.controller;

import com.demo.project_template.entity.SysUser;
import com.demo.project_template.service.SysUserService;
import com.demo.project_template.vo.request.sys.SysUserRequestVO;
import com.demo.project_template.vo.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-01-03 20:32:03
 */
@Api(tags = "后台用户模块")
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "创建或修改后台用户",notes = "有id为修改，不传id为创建，另外用户自己修改时不传roleIds")
    @PostMapping("/createSysUser")
    @ApiImplicitParam(value = "0=用户修改，1=管理员修改")
    public R<?> createSysUser(@RequestParam Integer updateSource,@RequestBody SysUserRequestVO sysUserRequestVO){
        if (!sysUserService.checkUserNameUnique(sysUserRequestVO.getId(),sysUserRequestVO.getUsername())){
            return R.error(500,"新增用户'" + sysUserRequestVO.getUsername() + "'失败，登录账号已存在");
        } else if (!sysUserService.checkPhoneUnique(sysUserRequestVO.getId(),sysUserRequestVO.getPhoneNumber())) {
            return R.error(500,"新增用户'" + sysUserRequestVO.getUsername() + "'失败，手机号码已存在");
        }
        sysUserService.createOrUpdateSysUser(updateSource,sysUserRequestVO);
        return R.success();
    }

    @ApiOperation(value = "删除后台用户")
    @PostMapping("/deleteSysUser")
    public R<?> deleteSysUser(@RequestParam Integer sysUserId){
        sysUserService.deleteSysUser(sysUserId);
        return R.success();
    }
}
